/**
 * 
 */
package GUI;

import java.awt.*;

import javax.swing.*;

import Utils.MailValidator;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

/**
 * @author aviat
 *
 */
public class AddWorkerGui extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JTextField userNameTextField;
	private JButton btnAdd;
	private JButton btnBack;
	private JComboBox<String> privilageComboBox;
	private JLabel iconlabel;
	private JLabel lblFirstName;
	private JLabel lblLastName;
	private JLabel lblPhoneNumber;
	private JLabel lblAddress;
	private JLabel lblEmail;
	private JLabel lblPosition;
	private JTextField FirstNametextField;
	private JTextField LastNametextField;
	private JTextField PhoneNumbertextField;
	private JTextField EmailtextField;
	private JComboBox<String> PositioncomboBox;
	private JTextArea AddresstextArea;
	private JFileChooser chooser;;
	
	
	private Object syncObject;
	private boolean toChangeAllowed = true;;
	
	public AddWorkerGui() {
		
		syncObject= new Object();
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		userNameTextField = new JTextField();
		userNameTextField.setFont(new Font("Tahoma", Font.BOLD, 12));
	
		chooser = new JFileChooser();
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AddWorkerGui.class.getResource("/resources/back22.png")));
		btnAdd = new JButton("Add");
		btnAdd.setIcon(new ImageIcon(AddWorkerGui.class.getResource("/resources/edit_add.png")));
		privilageComboBox = new JComboBox<String>();
		privilageComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				synchronized (syncObject) {
					if(isToChangeAllowed())
						filterList(PositioncomboBox,privilageComboBox.getSelectedItem().toString(),"POSITION");
				}
			}
		});
		privilageComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		PositioncomboBox = new JComboBox<String>();
		PositioncomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		PositioncomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				synchronized (syncObject) {
					if(isToChangeAllowed())
						filterList(privilageComboBox,PositioncomboBox.getSelectedItem().toString(),"PRIVILAGE");
				}
			}
		});
		FirstNametextField = new JTextField();
		FirstNametextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		LastNametextField = new JTextField();
		LastNametextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		PhoneNumbertextField = new JTextField();
		PhoneNumbertextField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent evt) {
				char c = evt.getKeyChar();
				if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE))){
					getToolkit().beep();
					evt.consume();
				}
			}
		});
		
		PhoneNumbertextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		EmailtextField = new JTextField();
		EmailtextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		AddresstextArea = new JTextArea();
		AddresstextArea.setFont(new Font("Courier New", Font.BOLD, 12));
		
		initialize();
	}

	private void initialize() {


		JLabel lblAddWorker = new JLabel("ADD WORKER");
		lblAddWorker.setForeground(new Color(0, 204, 204));
		lblAddWorker.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblAddWorker.setBounds(10, 11, 139, 44);
		add(lblAddWorker);
		
		JLabel lblName = new JLabel("Worker ID");
		lblName.setForeground(Color.WHITE);
		lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblName.setBounds(35, 222, 100, 27);
		add(lblName);
		
		JLabel lblPrivilage = new JLabel("Privilage");
		lblPrivilage.setForeground(Color.WHITE);
		lblPrivilage.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPrivilage.setBounds(339, 88, 58, 27);
		add(lblPrivilage);
		
		userNameTextField.setBounds(145, 221, 117, 30);
		add(userNameTextField);
		userNameTextField.setColumns(10);
		userNameTextField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent evt) {
				char c = evt.getKeyChar();
				if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE))){
					getToolkit().beep();
					evt.consume();
				}
			}
		});
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		btnAdd.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnAdd.setBounds(252, 397, 101, 27);
		add(btnAdd);
		
		privilageComboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Manager", "Worker", "Secretary"}));
		privilageComboBox.setBounds(407, 88, 144, 27);
		AutoCompletion.enable(this.privilageComboBox);
		add(privilageComboBox);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblFirstName = new JLabel("First Name");
		lblFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFirstName.setForeground(Color.WHITE);
		lblFirstName.setBounds(35, 90, 100, 27);
		add(lblFirstName);
		
		lblLastName = new JLabel("Last Name");
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblLastName.setForeground(Color.WHITE);
		lblLastName.setBounds(35, 155, 100, 27);
		add(lblLastName);
		
		lblPhoneNumber = new JLabel("Phone Number");
		lblPhoneNumber.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPhoneNumber.setForeground(Color.WHITE);
		lblPhoneNumber.setBounds(35, 295, 100, 27);
		add(lblPhoneNumber);
		
		lblAddress = new JLabel("Address");
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAddress.setForeground(Color.WHITE);
		lblAddress.setBounds(339, 295, 100, 27);
		add(lblAddress);
		
		lblEmail = new JLabel("Email");
		lblEmail.setForeground(Color.WHITE);
		lblEmail.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEmail.setBounds(339, 155, 74, 27);
		add(lblEmail);
		
		lblPosition = new JLabel("Position");
		lblPosition.setForeground(Color.WHITE);
		lblPosition.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPosition.setBounds(339, 222, 100, 27);
		add(lblPosition);
		
		
		FirstNametextField.setBounds(145, 92, 117, 27);
		add(FirstNametextField);
		FirstNametextField.setColumns(10);
		
		
		LastNametextField.setBounds(145, 155, 117, 27);
		add(LastNametextField);
		LastNametextField.setColumns(10);
		
		
		PhoneNumbertextField.setBounds(145, 295, 117, 27);
		add(PhoneNumbertextField);
		PhoneNumbertextField.setColumns(10);
		
		
		EmailtextField.setBounds(407, 155, 181, 27);
		add(EmailtextField);
		EmailtextField.setColumns(10);
		
		
		PositioncomboBox.setBounds(407, 226, 144, 27);
		PositioncomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Manager", "Secretary","Stills","Magnets","Video"}));
		add(PositioncomboBox);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(407, 303, 161, 66);
		add(scrollPane);
		
		
		scrollPane.setViewportView(AddresstextArea);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	private void filterList(JComboBox<String> combo, String selectedItem, String scope)
	{
		toChangeAllowed=false;
		synchronized (syncObject) {
			combo.removeAllItems();
			
			switch(scope)
			{
			case "PRIVILAGE":
				switch(selectedItem) 
				{
				case "Manager":
					combo.addItem("Manager");
					break;
				case "Secretary":
					combo.addItem("Secretary");
					break;
				default:
					combo.addItem("Worker");
					break;
				}
				break;
			case "POSITION":
				switch(selectedItem) 
				{
				case "Manager":
					combo.addItem("Manager");
					break;
				case "Secretary":
					combo.addItem("Secretary");
					break;
				default:
					combo.addItem("Stills");
					combo.addItem("Magnets");
					combo.addItem("Video");
					break;
				}
				break;
			
			}
			
		}
		toChangeAllowed=true;
	}
	
	/**
	* @return the toChangeAllowed
	* 
	*/
	private boolean isToChangeAllowed()
	{
		return toChangeAllowed;
	}
	/**
	 * @return the userNameTextField
	 */
	public JTextField getUserNameTextField() {
		return userNameTextField;
	}

	/**
	 * @param userNameTextField the userNameTextField to set
	 */
	public void setUserNameTextField(JTextField userNameTextField) {
		this.userNameTextField = userNameTextField;
	}

	/**
	 * @return the btnAdd
	 */
	public JButton getBtnAdd() {
		return btnAdd;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the privilageComboBox
	 */
	public JComboBox<String> getPrivilageComboBox() {
		return privilageComboBox;
	}

	/**
	 * @return the firstNametextField
	 */
	public JTextField getFirstNametextField() {
		return FirstNametextField;
	}

	/**
	 * @param firstNametextField the firstNametextField to set
	 */
	public void setFirstNametextField(JTextField firstNametextField) {
		FirstNametextField = firstNametextField;
	}

	/**
	 * @return the lastNametextField
	 */
	public JTextField getLastNametextField() {
		return LastNametextField;
	}

	/**
	 * @param lastNametextField the lastNametextField to set
	 */
	public void setLastNametextField(JTextField lastNametextField) {
		LastNametextField = lastNametextField;
	}

	/**
	 * @return the phoneNumbertextField
	 */
	public JTextField getPhoneNumbertextField() {
		return PhoneNumbertextField;
	}

	/**
	 * @param phoneNumbertextField the phoneNumbertextField to set
	 */
	public void setPhoneNumbertextField(JTextField phoneNumbertextField) {
		PhoneNumbertextField = phoneNumbertextField;
	}

	/**
	 * @return the emailtextField
	 */
	public JTextField getEmailtextField() {
		return EmailtextField;
	}

	/**
	 * @param emailtextField the emailtextField to set
	 */
	public void setEmailtextField(JTextField emailtextField) {
		EmailtextField = emailtextField;
	}

	/**
	 * @return the positioncomboBox
	 */
	public JComboBox<String> getPositioncomboBox() {
		return PositioncomboBox;
	}

	/**
	 * @param positioncomboBox the positioncomboBox to set
	 */
	public void setPositioncomboBox(JComboBox<String> positioncomboBox) {
		PositioncomboBox = positioncomboBox;
	}

	/**
	 * @return the addresstextArea
	 */
	public JTextArea getAddresstextArea() {
		return AddresstextArea;
	}

	/**
	 * @param addresstextArea the addresstextArea to set
	 */
	public void setAddresstextArea(JTextArea addresstextArea) {
		AddresstextArea = addresstextArea;
	}

	/**
	 * @param btnAdd the btnAdd to set
	 */
	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param privilageComboBox the privilageComboBox to set
	 */
	public void setPrivilageComboBox(JComboBox<String> privilageComboBox) {
		this.privilageComboBox = privilageComboBox;
	}
}
